package com.tzeng.hist.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.tzeng.hist.BaseApplication;
import com.tzeng.hist.R;
import com.tzeng.hist.fragment.TabFragment0;
import com.tzeng.hist.fragment.TabFragment1;
import com.tzeng.hist.fragment.TabFragment2;
import com.tzeng.hist.fragment.TabFragment3;
import com.tzeng.hist.manager.TitleManager;
import com.tzeng.hist.view.CustomViewPager;

public class MainActivity extends BaseActivity implements
		OnCheckedChangeListener {
	private RadioGroup mRadioGroup;
	private RadioButton mRadioButton1;
	private RadioButton mRadioButton2;
	private RadioButton mRadioButton3;
	private RadioButton mRadioButton4;
	private ImageView mImageView;
	private float mCurrentCheckedRadioLeft;// 当前被选中的RadioButton距离左侧的距离
	private HorizontalScrollView mHorizontalScrollView;// 上面的水平滚动控件

	private CustomViewPager mViewPager;
	private List<Fragment> mTabs = new ArrayList<Fragment>();
	private FragmentPagerAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		TitleManager.showTitle(this, null, R.string.app_name);

		mViewPager = (CustomViewPager) findViewById(R.id.id_viewpager);
		initDatas();
		mViewPager.setAdapter(mAdapter);

		iniController();
		mRadioGroup.setOnCheckedChangeListener(this);
		mRadioButton1.setChecked(true);
		mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();

	}

	private void initDatas() {
		TabFragment1 tabFragment1 = new TabFragment1();
		mTabs.add(tabFragment1);

		TabFragment2 tabFragment2 = new TabFragment2();
		mTabs.add(tabFragment2);

		TabFragment3 tabFragment3 = new TabFragment3();
		mTabs.add(tabFragment3);
		
		TabFragment0 tabFragment = new TabFragment0();
		mTabs.add(tabFragment);

		mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

			@Override
			public int getCount() {
				return mTabs.size();
			}

			@Override
			public Fragment getItem(int arg0) {
				return mTabs.get(arg0);
			}
		};

	}

	private void iniController() {
		// TODO Auto-generated method stub
		mRadioGroup = (RadioGroup) this.findViewById(R.id.radioGroup);
		mRadioButton1 = (RadioButton) this.findViewById(R.id.btn1);
		mRadioButton2 = (RadioButton) this.findViewById(R.id.btn2);
		mRadioButton3 = (RadioButton) this.findViewById(R.id.btn3);
		mRadioButton4 = (RadioButton) this.findViewById(R.id.btn4);

		mImageView = (ImageView) this.findViewById(R.id.img1);

		mHorizontalScrollView = (HorizontalScrollView) this
				.findViewById(R.id.horizontalScrollView);
	}

	/**
	 * RadioGroup点击CheckedChanged监听
	 */
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		AnimationSet _AnimationSet = new AnimationSet(true);
		TranslateAnimation _TranslateAnimation;

		Log.i("zj", "checkedid=" + checkedId);
		if (checkedId == R.id.btn1) {
			_TranslateAnimation = new TranslateAnimation(
					mCurrentCheckedRadioLeft, getResources().getDimension(
							R.dimen.rdo1), 0f, 0f);
			_AnimationSet.addAnimation(_TranslateAnimation);
			_AnimationSet.setFillBefore(false);
			_AnimationSet.setFillAfter(true);
			_AnimationSet.setDuration(100);
			mImageView.startAnimation(_AnimationSet);// 开始上面蓝色横条图片的动画切换
			mViewPager.setCurrentItem(0, false);

		} else if (checkedId == R.id.btn2) {
			_TranslateAnimation = new TranslateAnimation(
					mCurrentCheckedRadioLeft, getResources().getDimension(
							R.dimen.rdo2), 0f, 0f);

			_AnimationSet.addAnimation(_TranslateAnimation);
			_AnimationSet.setFillBefore(false);
			_AnimationSet.setFillAfter(true);
			_AnimationSet.setDuration(100);

			mImageView.startAnimation(_AnimationSet);
			mViewPager.setCurrentItem(1, false);

		} else if (checkedId == R.id.btn3) {
			_TranslateAnimation = new TranslateAnimation(
					mCurrentCheckedRadioLeft, getResources().getDimension(
							R.dimen.rdo3), 0f, 0f);

			_AnimationSet.addAnimation(_TranslateAnimation);
			_AnimationSet.setFillBefore(false);
			_AnimationSet.setFillAfter(true);
			_AnimationSet.setDuration(100);
			mImageView.startAnimation(_AnimationSet);
			mViewPager.setCurrentItem(2, false);

		} else if (checkedId == R.id.btn4) {
			_TranslateAnimation = new TranslateAnimation(
					mCurrentCheckedRadioLeft, getResources().getDimension(
							R.dimen.rdo4), 0f, 0f);

			_AnimationSet.addAnimation(_TranslateAnimation);
			_AnimationSet.setFillBefore(false);
			_AnimationSet.setFillAfter(true);
			_AnimationSet.setDuration(100);

			// mImageView.bringToFront();
			mImageView.startAnimation(_AnimationSet);
			mViewPager.setCurrentItem(3, false);
		}

		mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();// 更新当前蓝色横条距离左边的距离

		Log.i("zj", "getCurrentCheckedRadioLeft="
				+ getCurrentCheckedRadioLeft());
		Log.i("zj", "getDimension=" + getResources().getDimension(R.dimen.rdo2));

		mHorizontalScrollView.smoothScrollTo((int) mCurrentCheckedRadioLeft
				- (int) getResources().getDimension(R.dimen.rdo2), 0);
	}

	/**
	 * 获得当前被选中的RadioButton距离左侧的距离
	 */
	private float getCurrentCheckedRadioLeft() {
		// TODO Auto-generated method stub
		if (mRadioButton1.isChecked()) {

			return getResources().getDimension(R.dimen.rdo1);
		} else if (mRadioButton2.isChecked()) {

			return getResources().getDimension(R.dimen.rdo2);
		} else if (mRadioButton3.isChecked()) {

			return getResources().getDimension(R.dimen.rdo3);
		} else if (mRadioButton4.isChecked()) {

			return getResources().getDimension(R.dimen.rdo4);
		}
		return 0f;
	}

	private long exitTime;

	@Override
	public void onBackPressed() {
		// 2次返回退出
		if ((System.currentTimeMillis() - exitTime) > 2000) {
			Toast.makeText(getApplicationContext(), R.string.msg_exit,
					Toast.LENGTH_SHORT).show();
			exitTime = System.currentTimeMillis();
		} else {
			((BaseApplication) getApplication()).exit();
		}
	}

}
